
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.pats.thesisrobotica.robot.RR.sensors.listeners.threads;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.sensors.Sensor;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InitializationException;

import lejos.nxt.comm.RConsole;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;

/**
 *
 * @author Admin
 */
public class ListenerThread extends Thread {
    static final ListenerThread      singleton = new ListenerThread();
    private static ArrayList<Sensor> sensorList;

    private ListenerThread() {
        sensorList = new ArrayList();
    }

    public static ListenerThread get() {
        return singleton;
    }

    public void addSensor(Sensor sensor) {
        RConsole.println("ListenerThread:addSensor(): Adding sensor " + sensor.getId());
        sensorList.add(sensor);
    }

    @Override
    public void run() {
        int    i = 0;
        Sensor currentSensor;

        RConsole.println("ListenerThread:run(): Starting");

            while (true) {
                try {
                    if(!(sensorList.size() == 0)) {
                        currentSensor = sensorList.get(i);
                        currentSensor.compareThreshold(currentSensor.getRealValue());
                        if (++i == sensorList.size()) {
                            i = 0;
                        }
                    }
                } catch (InitializationException ex) {
                    RConsole.println("InitializationException in run()");
                }
            }
        
    }
}
